home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdLinkChecker.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  5.1 KB  |  170 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  *   Akkana Peck (akkana@netscape.com)
  22.  *   Charles Manxke (cmanske@netscape.com)
  23.  */
  24.  
  25. // Variables used across all the links being checked:
  26. var gNumLinksToCheck = 0;     // The number of nsILinkCheckers
  27. var gLinksBeingChecked = {};  // Array of nsIURICheckers
  28. var gURIRefObjects = {};      // Array of nsIURIRefObjects
  29. var gNumLinksCalledBack = 0;
  30. var gStartedAllChecks = false;
  31. var gLinkCheckTimerID = 0;
  32.  
  33. // Implement nsIRequestObserver:
  34. var gRequestObserver =
  35. {
  36.   // urichecker requires that we have an OnStartRequest even tho it's a nop.
  37.   onStartRequest: function(request, ctxt) { },
  38.  
  39.   // onStopRequest is where we really handle the status.
  40.   onStopRequest: function(request, ctxt, status)
  41.   {
  42.     var linkChecker = request.QueryInterface(Components.interfaces.nsIURIChecker);
  43.     if (linkChecker)
  44.     {
  45.       gNumLinksCalledBack++;
  46.       linkChecker.status = status;
  47.       for (var i = 0; i < gNumLinksCalledBack; i++)
  48.       {
  49.         if (linkChecker ==  gLinksBeingChecked[i])
  50.           gLinksBeingChecked[i].status = status;
  51.       }
  52.  
  53.       if (gStartedAllChecks && gNumLinksCalledBack >= gNumLinksToCheck)
  54.       {
  55.         clearTimeout(gLinkCheckTimerID);
  56.         LinkCheckTimeOut();
  57.       }
  58.     }
  59.   }
  60. }
  61.  
  62. function Startup()
  63. {
  64.   if (!InitEditorShell())
  65.     return;
  66.  
  67.   // Set window location relative to parent window (based on persisted attributes)
  68.   SetWindowLocation();
  69.   // Get all objects that refer to other locations
  70.   var objects = editorShell.GetLinkedObjects();
  71.  
  72.   // Loop over the nodes that have links:
  73.   for (var i = 0; i < objects.Count(); i++)
  74.   {
  75.     var refobj = objects.GetElementAt(gNumLinksToCheck).QueryInterface(Components.interfaces.nsIURIRefObject);
  76.     // Loop over the links in this node:
  77.     if (refobj)
  78.     {
  79.       try {
  80.         var uri;
  81.         while ((uri = refobj.GetNextURI()))
  82.         {
  83.           // Use the real class in netlib:
  84.           // Note that there may be more than one link per refobj
  85.           gURIRefObjects[gNumLinksToCheck] = refobj;
  86.  
  87.           // Make a new nsIURIChecker
  88.           gLinksBeingChecked[gNumLinksToCheck]
  89.             = Components.classes["@mozilla.org/network/urichecker;1"]
  90.                 .createInstance()
  91.                   .QueryInterface(Components.interfaces.nsIURIChecker);
  92.  
  93.           // XXX Calling this is related to crash deleting gLinksBeingChecked when dialog is closed
  94.           //     (if this isn't called, no crash)
  95.           gLinksBeingChecked[gNumLinksToCheck].asyncCheckURI(uri, gRequestObserver, null,
  96.                                  Components.interfaces.nsIRequest.LOAD_NORMAL);
  97.           gNumLinksToCheck++;
  98.         };
  99.       } catch (e) {
  100. dump(" Exception ERROR in Link checker. e.result="+e.result+", gNumLinksToCheck="+gNumLinksToCheck+"\n");
  101.       }
  102.     }
  103.   }
  104.   // Done with the loop, now we can be prepared for the finish:
  105.   gStartedAllChecks = true;
  106.  
  107.   // Start timer to limit how long we wait for link checking
  108.   gLinkCheckTimerID = setTimeout("LinkCheckTimeOut()", 5000);
  109. }
  110.  
  111. function LinkCheckTimeOut()
  112. {
  113.   // We might have gotten here via a late timeout
  114.   if (gNumLinksToCheck <= 0)
  115.     return;
  116.   gLinkCheckTimerID = 0;
  117.  
  118. dump("*** LinkCheckTimeout: Heard from " + gNumLinksCalledBack + " of "+gNumLinksToCheck + "\n");
  119.  
  120.   gNumLinksToCheck = 0;
  121.   gStartedAllChecks = false;
  122.   if ("length" in gLinksBeingChecked)
  123.   {
  124.     for (var i=0; i < gLinksBeingChecked.length; i++)
  125.     {
  126.       var linkChecker = gLinksBeingChecked[i].QueryInterface(Components.interfaces.nsIURIChecker);
  127.       // nsIURIChecker status values:
  128.       // NS_BINDING_SUCCEEDED     link is valid
  129.       // NS_BINDING_FAILED        link is invalid (gave an error)
  130.       // NS_BINDING_ABORTED       timed out, or cancelled
  131.       switch (linkChecker.status)
  132.       {
  133.         case 0:           // NS_BINDING_SUCCEEDED
  134.           dump("   " + linkChecker.name + " OK!\n");
  135.           break;
  136.         case 0x804b0001:  // NS_BINDING_FAILED
  137.           dump(">> " + linkChecker.name + " is broken\n");
  138.           break;
  139.         case 0x804b0002:   // NS_BINDING_ABORTED
  140.           dump(">> " + linkChecker.name + " timed out\n");
  141.           break;
  142.         default:
  143.           dump(">> " + linkChecker.name + " not checked\n");
  144.           break;
  145.       }
  146.     }
  147.   }
  148. }
  149.  
  150. function ChangeUrl()
  151. {
  152. }
  153.  
  154. function onAccept()
  155. {
  156.   SaveWindowLocation();
  157.   return true; // do close the window
  158. }
  159.  
  160. function onCancelLinkChecker()
  161. {
  162.   if (gLinkCheckTimerID)
  163.     clearTimeout(gLinkCheckTimerID);
  164.  
  165. /*
  166.   LinkCheckTimeOut();
  167. */
  168.   return onCancel();
  169. }
  170.